package com.example.controller;

import com.aliyun.oss.OSSClient;
import com.example.common.AliyunOSSClientUtil;
import com.example.common.Result;
import com.example.entity.Article;
import com.example.entity.User;
import com.example.entity.UserActions;
import com.example.service.ArticleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @PostMapping("/upload")
    public Result upload(@RequestPart("file") MultipartFile file){
        OSSClient oss = AliyunOSSClientUtil.getOSSClient();
        String url = AliyunOSSClientUtil.uploadObject2OSS(oss,file,"zxtbysj","upload/");
        return Result.success(url);
    }
    @PostMapping("/add")
    public Result addArticle(@RequestBody Article article){
        return articleService.addArticle(article);
    }
    @PostMapping("/listByUser")
    public Result listByUser(@RequestBody User user){
        return articleService.listByUser(user);
    }
    @PostMapping("/search")
    public Result searchArticle(@RequestBody Article article){
        return articleService.searchArticle(article);
    }
    @PostMapping("/get")
    public Result getArticle(@RequestBody Article article){
        return articleService.getArticle(article);
    }
    @PostMapping("/like")
    public Result likeArticle(@RequestBody UserActions userActions){
        return articleService.likeArticle(userActions);
    }
    @PostMapping("/unlike")
    public Result unLikeArticle(@RequestBody UserActions userActions){
        return articleService.unLikeArticle(userActions);
    }
    @PostMapping("/update")
    public Result update(@RequestBody Article article){
        return articleService.update(article);
    }
    @PostMapping("/listAll")
    public Result listAll(){
        return articleService.listAll();
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody Article article){
        return articleService.delete(article);
    }
}
